package com.daocao.jy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.daocao.jy.R;
import com.daocao.jy.base.BaseActivity;
import com.daocao.jy.constant.IdCardBitmap;
import com.daocao.jy.view.V19FrameLayout;
import com.lljjcoder.citypickerview.widget.CityPicker;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class OilAgencyActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.txt_province)
    TextView txtProvince;
    @BindView(R.id.txt_city)
    TextView txtCity;
    @BindView(R.id.txt_area)
    TextView txtArea;
    @BindView(R.id.edit_address)
    EditText editAddress;
    @BindView(R.id.img_idcard_front)
    ImageView imgIdcardFront;
    @BindView(R.id.img_idcard_back)
    ImageView imgIdcardBack;
    @BindView(R.id.txt_money)
    TextView txtMoney;
    @BindView(R.id.txt_tips)
    TextView txtTips;
    @BindView(R.id.btn_agree_check)
    ImageView btnAgreeCheck;
    @BindView(R.id.txt_service_shihua)
    TextView txtServiceShihua;
    @BindView(R.id.txt_service_app)
    TextView txtServiceApp;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    private static final String DEFAULT_NAME1 = "front_image.jpg";
    private static final String DEFAULT_NAME2 = "back_image.jpg";
    private Unbinder unbiner;
    private CityPicker cityPicker;
    private int takeType;
    private boolean isAgree = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_oil_agency);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("代办油卡");
        cityPicker = new CityPicker.Builder(this)
                .title("收卡地址")
                .cancelTextColor("#1792F3")
                .confirTextColor("#1792F3")
                .titleTextColor("#000000")
                .build();
        cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
            @Override
            public void onSelected(String... citySelected) {
                if (!TextUtils.isEmpty(citySelected[0])) {
                    txtProvince.setText(citySelected[0]);
                }
                if (!TextUtils.isEmpty(citySelected[1])) {
                    txtCity.setText(citySelected[1]);
                }
                if (!TextUtils.isEmpty(citySelected[2])) {
                    txtArea.setText(citySelected[2]);
                }
            }

            @Override
            public void onCancel() {

            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
            if (resultCode == -1) {
                try {

                    if (takeType == 1) {
                        if (IdCardBitmap.bitmap != null) {
                            imgIdcardFront.setImageBitmap(IdCardBitmap.bitmap);
                        }
                    } else if (takeType == 2) {

                        if (IdCardBitmap.bitmap != null) {
                            imgIdcardBack.setImageBitmap(IdCardBitmap.bitmap);
                        }
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void startTakePhoto(int type) {
        checkPermission(new CheckPermListener() {
            @Override
            public void superPermission() {
                takeType = type;
                if (type == 1) {
                    startActivity(DEFAULT_NAME1);
                } else if (type == 2) {
                    startActivity(DEFAULT_NAME2);
                }
            }
        }, R.string.camera_storage_card_p, R.string.get_cammra, android.Manifest.permission.CAMERA, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    private void startActivity(String paramString) {
        Intent localIntent = new Intent(this, CameraActivity.class);
        localIntent.putExtra("path", "/sdcard/pictures");
        localIntent.putExtra("name", paramString);
        localIntent.putExtra("type", "idcard");
        startActivityForResult(localIntent, 100);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (cityPicker != null) {
            cityPicker.hide();
            cityPicker.destroyPicker();
            cityPicker = null;
        }
        if (IdCardBitmap.bitmap != null) {
            IdCardBitmap.bitmap.recycle();
            IdCardBitmap.bitmap = null;
        }
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.txt_province, R.id.txt_city, R.id.txt_area, R.id.img_idcard_front, R.id.img_idcard_back, R.id.btn_agree_check, R.id.txt_service_shihua, R.id.txt_service_app, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.txt_province:
                cityPicker.show();
                break;
            case R.id.txt_city:
                cityPicker.show();
                break;
            case R.id.txt_area:
                cityPicker.show();
                break;
            case R.id.img_idcard_front:
                startTakePhoto(1);
                break;
            case R.id.img_idcard_back:
                startTakePhoto(2);
                break;
            case R.id.btn_agree_check:
                if (isAgree) {
                    btnAgreeCheck.setImageResource(R.mipmap.img_oil_agree_uncheck);
                } else {
                    btnAgreeCheck.setImageResource(R.mipmap.img_oil_agree_check);
                }
                isAgree=!isAgree;
                break;
            case R.id.txt_service_shihua:
                break;
            case R.id.txt_service_app:
                break;
            case R.id.btn_submit:
                break;
        }
    }
}
